const fs = require('fs');
const path = require('path');

export function deleteFolderRecursive(folderPath) {
  if (fs.existsSync(folderPath)) {
    fs.readdirSync(folderPath).forEach((file) => {
      const curPath = path.join(folderPath, file);
      if (fs.lstatSync(curPath).isDirectory()) {
        // 是文件夹
        this.deleteFolderRecursive(curPath);
      } else {
        // 是文件
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(folderPath);
    console.log(`文件夹 ${folderPath} 删除成功`);
  } else {
    console.log(`文件夹 ${folderPath} 不存在`);
  }
}

export function copyFolderRecursiveSync(source, target) {
  if (!fs.existsSync(target)) {
    fs.mkdirSync(target);
  }

  const files = fs.readdirSync(source);
  files.forEach((file) => {
    console.log(file)
    const currentFilePath = path.join(source, file);
    const targetFilePath = path.join(target, file);
    if (fs.lstatSync(currentFilePath).isDirectory()) {
      copyFolderRecursiveSync(currentFilePath, targetFilePath);
    } else {
      fs.copyFileSync(currentFilePath, targetFilePath);
    }
  });
}
